package xyz.quanguanjia.masterboy;

import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import xyz.quanguanjia.masterboy.model.MBDAccountInfoModel;
import xyz.quanguanjia.masterboy.util.StringUtils;

// 定义单元格类型的枚举类

public class MineListAdapter extends RecyclerView.Adapter {

    public static final int TYPE_AVATAR = 1;
    public static final int TYPE_SETTING = 2;
    public static final int TYPE_PUBLISH = 3;
    public static final int TYPE_INTEGRATE = 4;

    private List<MineCellItemModel> models;

    public static List<MineCellItemModel> defaultModels() {
        List<MineCellItemModel> ms = new ArrayList<MineCellItemModel>();

        MBDAccountInfoModel accountInfoModel = MBDAccountInfoModel.current();
        if (accountInfoModel != null) {
            MineCellItemModel model1 = new MineCellItemModel();
            if (StringUtils.isNull(accountInfoModel.getRealName())) {
                model1.title = "用户" + accountInfoModel.getUserId();
            } else {
                model1.title = accountInfoModel.getRealName();
            }
            if (StringUtils.isNull(accountInfoModel.getAvatarUrl())) {
                model1.image = "";
            } else {
                model1.image = accountInfoModel.getAvatarUrl();
            }
            model1.type = MineListAdapter.TYPE_AVATAR;
            ms.add(model1);

        } else {
            MineCellItemModel model1 = new MineCellItemModel();
            model1.title = "点击登录";
            model1.image = MBDPath2URLUtils.placeHolderAvatarPath;
            model1.type = MineListAdapter.TYPE_AVATAR;
            ms.add(model1);
        }

        MineCellItemModel mypostModel1 = new MineCellItemModel();
        mypostModel1.title = "我创建的";
        mypostModel1.image = MBDPath2URLUtils.filePathPrefix + "ico_publish";
        mypostModel1.type = MineListAdapter.TYPE_PUBLISH;
        ms.add(mypostModel1);

        MineCellItemModel integrateModel1 = new MineCellItemModel();
        integrateModel1.title = "积分";
        integrateModel1.image = MBDPath2URLUtils.filePathPrefix + "ico_integrate_sm";
        integrateModel1.type = MineListAdapter.TYPE_INTEGRATE;
        ms.add(integrateModel1);

        MineCellItemModel model2 = new MineCellItemModel();
        model2.title = "设置";
        model2.image = MBDPath2URLUtils.filePathPrefix + "settings_light";
        model2.type = MineListAdapter.TYPE_SETTING;
        ms.add(model2);
        return ms;
    }

    public MineListAdapter(List<MineCellItemModel> models) {

        this.models = models;
    }

    public void setModels(List<MineCellItemModel> models) {
        this.models = models;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 创建列表项视图

        View itemView;
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());

        switch (viewType) {
            case MineListAdapter.TYPE_AVATAR:
                itemView = inflater.inflate(R.layout.layout_cell_avatar, parent, false);
                return new MBDMineAvatarViewHolder(itemView);
            case MineListAdapter.TYPE_SETTING:
            case MineListAdapter.TYPE_PUBLISH:
            case MineListAdapter.TYPE_INTEGRATE:
                itemView = inflater.inflate(R.layout.layout_cell_mine, parent, false);
                return new MBDMineCommonViewHolder(itemView);
            default:
                itemView = inflater.inflate(R.layout.layout_cell_default, parent, false);
                return new MBDDefaultViewHolder(itemView);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        // 绑定数据到列表项视图
        MineCellItemModel detail = this.models.get(position);
        switch (detail.type) {
            case MineListAdapter.TYPE_AVATAR:
                ((MBDMineAvatarViewHolder) holder).bind(detail);
                break;
            case MineListAdapter.TYPE_SETTING:
            case MineListAdapter.TYPE_PUBLISH:
            case MineListAdapter.TYPE_INTEGRATE:
                ((MBDMineCommonViewHolder) holder).bind(detail);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return this.models.size();
    }

    @Override
    public int getItemViewType(int position) {
        return models.get(position).type;
    }

    public static class MBDMineAvatarViewHolder extends RecyclerView.ViewHolder {
        private TextView titleTextView;
        private ImageView avatar;

        public MBDMineAvatarViewHolder(@NonNull View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.text_mine_title);
            avatar = itemView.findViewById(R.id.image_mine_avatar);
        }

        public void bind(MineCellItemModel detail) {
            // 设置列表项视图的数据
            titleTextView.setText(detail.title);
            if (null == detail.image) {
                detail.image = "";
            }
            String imageUrl = MBDPath2URLUtils.wrapAvatarURL(detail.image);
            Glide.with(MBDApplication.getContext())
                    .load(imageUrl)
                    .apply(RequestOptions.circleCropTransform())
                    .placeholder(R.drawable.ico_appicon)
                    .error(R.drawable.ico_appicon)
                    .into(avatar);
        }
    }

    public static class MBDMineCommonViewHolder extends RecyclerView.ViewHolder {
        private TextView titleTextView;
        private ImageView leftIco;

        public MBDMineCommonViewHolder(@NonNull View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.text_mine_title);
            leftIco = itemView.findViewById(R.id.image_mine_left);
        }

        public void bind(MineCellItemModel detail) {
            // 设置列表项视图的数据
            titleTextView.setText(detail.title);

            boolean usePlaceHolder = detail.image.startsWith(MBDPath2URLUtils.filePathPrefix);
            if (usePlaceHolder) {
                String imageName = detail.image.replaceAll(MBDPath2URLUtils.filePathPrefix, "");
                Resources resources = MBDApplication.getContext().getResources();
                String packageName = MBDApplication.getContext().getPackageName();
                int resId = resources.getIdentifier(imageName, "drawable", packageName);
                leftIco.setImageResource(resId);
            } else {
                String imageUrl = MBDPath2URLUtils.wrapAvatarURL(detail.image);

                int cornerRadius = 10; // 圆角半径，单位为像素
                RequestOptions options = new RequestOptions().transform(new CenterCrop(), new RoundedCorners(cornerRadius));

                Glide.with(MBDApplication.getContext()).load(imageUrl).apply(options).into(leftIco);
            }

        }
    }

    public static class MBDDefaultViewHolder extends RecyclerView.ViewHolder {

        public MBDDefaultViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void bind(MineCellItemModel detail) {
            // 设置列表项视图的数据

        }

    }
}
